package com.administrator.cloudapplication.activitys;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.support.v4.view.ViewPager;
import android.widget.CompoundButton;
import android.widget.RadioButton;

import com.administrator.cloudapplication.R;
import com.administrator.cloudapplication.adapter.MyFragmentPagerAdapter;
import com.administrator.cloudapplication.base.BaseActivity;

/*文件名：MainActivity
 * 作者：祁晓峰
 * 时间：2017/04/27
 *描述：程序的主页面，由一组复选按钮（RadioButton）和一个Viewpager构成，
 *      Viewpager由四个Fragment组成四个页面，每个按钮对应一个Fragment的页面。
 */
public class MainActivity extends BaseActivity implements CompoundButton.OnCheckedChangeListener, ViewPager.OnPageChangeListener {

    RadioButton rb_news, rb_media, rb_near, rb_more;//获取复选按钮
    ViewPager mViewPager;//获取ViewPager


    /**
     * 视图的初始化
     */
    protected void initView() {
        setContentView(R.layout.activity_cloud);//加载布局文件
        initTitleView();//实现基类BaseActivity的标题初始化方法
        //布局的所有控件实例化
        rb_news = (RadioButton) findViewById(R.id.rb_infomation);
        rb_media = (RadioButton) findViewById(R.id.rb_media);
        rb_near = (RadioButton) findViewById(R.id.rb_near);
        rb_more = (RadioButton) findViewById(R.id.rb_more);
        mViewPager = (ViewPager) findViewById(R.id.vp_cloud);
        //为复选按钮设置选中的监听事件，继承系统的OnCheckedChangeListener方法，在本类中实现
        rb_media.setOnCheckedChangeListener(this);
        rb_more.setOnCheckedChangeListener(this);
        rb_near.setOnCheckedChangeListener(this);
        rb_news.setOnCheckedChangeListener(this);
        mViewPager.addOnPageChangeListener(this);
        rb_news.setChecked(true);//设置第一个复选按钮默认选中
        mViewPager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager()));//把四个FragMent页面绑定到ViewPager控件上
        //将标题栏左边的回撤箭头动态替换为用户头像
        Drawable drawable = getResources().getDrawable(R.mipmap.ic_person);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        tvLeft.setCompoundDrawables(null, null, drawable, null);
    }


    @Override
    protected void initData() {
    }

    @Override
    protected void onLeftClick() {
        //点击标题栏左边的用户头像跳转到登陆界面
        Intent intent = new Intent(MainActivity.this, LoginActivity.class);
        startActivity(intent);
    }

    @Override
    protected void onRightClick() {

    }


    public void onCheckedChanged(CompoundButton buttonView, boolean b) {
        //将复选按钮的点击事件与ViewPager绑定起来
        if (b) {
            switch (buttonView.getId()) {
                case (R.id.rb_infomation):
                    tvTitle.setText("资讯");
                    mViewPager.setCurrentItem(0);
                    break;
                case (R.id.rb_media):
                    tvTitle.setText("媒体");
                    mViewPager.setCurrentItem(1);
                    break;
                case (R.id.rb_near):
                    tvTitle.setText("商城");
                    mViewPager.setCurrentItem(2);
                    break;
                case (R.id.rb_more):
                    tvTitle.setText("更多");
                    mViewPager.setCurrentItem(3);
                    break;
            }
        }

    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    }

    @Override
    public void onPageSelected(int position) {
        //将ViewPager的滑动时间与复选按钮绑定起来
        switch (position) {
            case (0):
                rb_news.setChecked(true);
                break;
            case (1):
                rb_media.setChecked(true);
                break;
            case (2):
                rb_near.setChecked(true);
                break;
            case (3):
                rb_more.setChecked(true);
                break;
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {
    }
}
